'use strict';

const fs = require('fs');
const path = require('path');
const {
    app: electronApp, dialog, shell, Notification,
} = require('electron');
const {windowService} = require('../service/os/window');
const {getMainWindow} = require("ee-core/electron");
const {logger} = require("ee-core/log");
/**
 * example
 * @class
 */
class OsController {

    /**
     * All methods receive two parameters
     * @param args Parameters transmitted by the frontend
     * @param event - Event are only available during IPC communication. For details, please refer to the controller documentation
     */

    /**
     * 消息提示对话框
     */
    messageShow() {
        dialog.showMessageBoxSync({
            type: 'info', // "none", "info", "error", "question" 或者 "warning"
            title: 'Custom Title',
            message: 'Customize message content',
            detail: 'Other additional information'
        })

        return 'Opened the message box';
    }

    /**
     * 消息提示和确认对话框
     */
    messageShowConfirm() {
        const res = dialog.showMessageBoxSync({
            type: 'info',
            title: 'Custom Title',
            message: 'Customize message content',
            detail: 'Other additional information',
            cancelId: 1, // Index of buttons used to cancel dialog boxes
            defaultId: 0, // Set default selected button
            buttons: ['confirm', 'cancel'],
        })
        let data = (res === 0) ? 'click the confirm button' : 'click the cancel button';

        return data;
    }

    /**
     * 选择目录
     */
    selectFolder() {
        const filePaths = dialog.showOpenDialogSync({
            properties: ['openDirectory', 'createDirectory']
        });

        if (!filePaths) {
            return null
        }

        return filePaths[0];
    }

    /**
     * 开放目录
     */
    openDirectory(args) {
        const {id} = args;
        if (!id) {
            return false;
        }
        let dir = '';
        if (path.isAbsolute(id)) {
            dir = id;
        } else {
            dir = electronApp.getPath(id);
        }

        shell.openPath(dir);
        return true;
    }

    /**
     * 选择图片
     */
    selectPic() {
        const filePaths = dialog.showOpenDialogSync({
            title: 'select pic',
            properties: ['openFile'],
            filters: [
                {name: 'Images', extensions: ['jpg', 'png', 'gif']},
            ]
        });
        if (!filePaths) {
            return null
        }

        try {
            const data = fs.readFileSync(filePaths[0]);
            const pic = 'data:image/jpeg;base64,' + data.toString('base64');
            return pic;
        } catch (err) {
            console.error(err);
            return null;
        }
    }

    /**
     * 打开一个新窗口
     */
    createWindow(args) {
        const wcid = windowService.createWindow(args);
        return wcid;
    }

    /**
     * 获取窗口内容id
     */
    getWCid(args) {
        const wcid = windowService.getWCid(args);
        return wcid;
    }

    /**
     * 通过主进程的传输实现两个窗口之间的通信
     */
    window1ToWindow2(args, event) {
        windowService.communicate(args, event);
        return;
    }

    /**
     * 通过主进程的传输实现两个窗口之间的通信
     */
    window2ToWindow1(args, event) {
        windowService.communicate(args, event);
        return;
    }

    /**
     * 创建系统通知
     */
    sendNotification(args, event) {
        const {title, subtitle, body, silent} = args;

        if (!Notification.isSupported()) {
            return '当前系统不支持通知';
        }

        let options = {};
        if (!title) {
            options.title = title;
        }
        if (!subtitle) {
            options.subtitle = subtitle;
        }
        if (!body) {
            options.body = body;
        }
        if (!silent) {
            options.silent = silent;
        }
        windowService.createNotification(options, event);

        return true
    }

    /**
     * 关闭窗口
     */
    closeWindow(args) {
        windowService.closeWindow(args);
    }

    /**
     * 最小化窗口
     */
    minimizeWindow(args) {
        windowService.minimizeWindow(args);
    }

    /**
     * 最大化窗口
     */
    maximizeWindow(args) {
        windowService.maximizeWindow(args);
    }

    /**
     * 还原窗口
     */
    restoreWindow(args) {
        windowService.restoreWindow(args);
    }

    setPositionWindow(args) {
        windowService.setPositionWindow(args, true)
    }

    getPositionWindow(args) {
        let positionWindow = windowService.getPositionWindow(args);
        return {x: positionWindow[0], y: positionWindow[1]}
    }
    getConfig(args){
        return windowService.getConfig(args);
    }
}

OsController.toString = () => '[class OsController]';

module.exports = OsController;  